import {Component, OnInit, Input, EventEmitter, Output} from '@angular/core';
import { Carrier } from '../carrier';

import { ActivatedRoute, Params } from '@angular/router';
import { CarrierService } from '../carrier.service';

@Component({
    selector: 'carrier-detail',
    templateUrl: './carrier-detail.component.html',
    // styleUrls: ['./carrier-detail.component.css']
})

export class CarrierDetailComponent implements OnInit {
    @Input()
    carrier: Carrier;
    @Output() onVoted = new EventEmitter<boolean>();

    constructor(
        private carrierService: CarrierService,
        private router: ActivatedRoute) {

    }

    ngOnInit() {
        this.router.params.forEach((params: Params) => {
            let carrier_id = +params['carrier_id'];
            this.carrierService.getCarrier(carrier_id).then(carrier => this.carrier = carrier);
        });
    }

    goBack() {
        window.history.back();
    }

    save(): void {
        this.carrierService.save(this.carrier).then(() => this.cancel());
    }

    delete(): void {
        this.carrierService.delete(this.carrier).then(() => this.cancel());
    }

    cancel(): void {
        this.carrier = null;
       this.onVoted.emit();
    }
}
